package main

import (
	"github.com/xuri/excelize"
	"fmt"
	"match-spider/util/boltdb"
	"github.com/boltdb/bolt"
	"strings"
	"strconv"
)

/**
导出比赛数据到excel

1、澳门盘口初盘终盘2.75，大球初盘到终盘赔率升0.05=小球
 */

func asia( fn func(map[string][]string,int))  {
	num:=0
	company:=[]string{
		"澳门","ＳＢ","Bet365","立博","韦德","易胜博","明陞","10BET","金宝博","12bet/大发","利记","盈禾","18Bet",
	}
	boltdb.ScanTx("matchs-2016", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			b.ForEach(func(k, v []byte) error {
				content:=string(v)
				s:=strings.Split(content,"^")
				league:=s[1]
				if league=="英冠"{

						m:=map[string][]string{
							"match":s,
						}
						boltdb.Get("asia-2016",league,string(k), func(data string) {
							if data!=""{
								num++
								odds:=strings.Split(data,"!")
								for _,v:=range odds {
									d := strings.Split(v, "^")
									name := d[0]
									slice:=[]string{d[2],d[3],d[4],d[5],d[6],d[7],}
									if strings.Compare(name,"澳门")==0{
										m["澳门"]=slice
									}
									if strings.Compare(name,"ＳＢ")==0{
										m["ＳＢ"]=slice
									}
									if strings.Compare(name,"Bet365")==0{
										m["Bet365"]=slice
									}
									if strings.Compare(name,"立博")==0{
										m["立博"]=slice
									}
									if strings.Compare(name,"韦德")==0{
										m["韦德"]=slice
									}
									if strings.Compare(name,"易胜博")==0{
										m["易胜博"]=slice
									}
									if strings.Compare(name,"明陞")==0{
										m["明陞"]=slice
									}
									if strings.Compare(name,"10BET")==0{
										m["10BET"]=slice
									}
									if strings.Compare(name,"金宝博")==0{
										m["金宝博"]=slice
									}
									if strings.Compare(name,"12bet/大发")==0{
										m["12bet/大发"]=slice
									}
									if strings.Compare(name,"利记")==0{
										m["利记"]=slice
									}
									if strings.Compare(name,"盈禾")==0{
										m["盈禾"]=slice
									}
									if strings.Compare(name,"18Bet")==0{
										m["18Bet"]=slice
									}
								}
								for _,v:=range company{
									if m[v]==nil{
										m[v]=[]string{
											"","","","","","",
										}
									}
								}
								fn(m,num)
							}

						})


				}

				return nil
			})
			return nil
		})

	})
	fmt.Println(num)
}
func main()  {


	xlsx := excelize.NewFile()

	categories := map[string]string{
		"A1": "编号",
		"B1": "联赛",
		"C1": "时间",
		"D1": "主队",
		"E1": "进球",
		"F1": "客队",
		"G1": "进球",
		"H1": "亚盘",
		"I1": "大小",
		"J1": "澳门",
		"K1": "",
		"L1": "",
		"M1": "",
		"N1": "",
		"O1": "",
		"P1": "ＳＢ",
		"Q1": "",
		"R1": "",
		"S1": "",
		"T1": "",
		"U1": "",
		"V1": "Bet365",//
		"W1": "",
		"X1": "",
		"Y1": "",
		"Z1": "",
		"AA1": "",
		"AB1": "立博",//
		"AC1": "",
		"AD1": "",
		"AE1": "",
		"AF1": "",
		"AG1": "",
		"AH1": "韦德",//
		"AI1": "",
		"AJ1": "",
		"AK1": "",
		"AL1": "",
		"AM1": "",
		"AN1": "易胜博",//
		"AO1": "",
		"AP1": "",
		"AQ1": "",
		"AR1": "",
		"AS1": "",
		"AT1": "明陞",//
		"AU1": "",
		"AV1": "",
		"AW1": "",
		"AX1": "",
		"AY1": "",
		"AZ1": "10BET",//
		"BA1": "",
		"BB1": "",
		"BC1": "",
		"BD1": "",
		"BE1": "",
		"BF1": "金宝博",//
		"BG1": "",
		"BH1": "",
		"BI1": "",
		"BJ1": "",
		"BK1": "",
		"BL1": "12bet/大发",//
		"BM1": "",
		"BN1": "",
		"BO1": "",
		"BP1": "",
		"BQ1": "",
		"BR1": "利记",//
		"BS1": "",
		"BT1": "",
		"BU1": "",
		"BV1": "",
		"BW1": "",
		"BX1": "盈禾",//
		"BY1": "",
		"BZ1": "",
		"CA1": "",
		"CB1": "",
		"CC1": "",
		"CD1": "18Bet",//
		"CE1": "",
		"CF1": "",
		"CG1": "",
		"CH1": "",
		"CI1": "",
	}

	for k,v:=range categories{
		xlsx.SetCellValue("Sheet1", k, v)
	}
	asia(func(data map[string][]string,i int) {
		match:=data["match"]
		odds1:=data["澳门"]
		odds2:=data["ＳＢ"]
		odds3:=data["Bet365"]
		odds4:=data["立博"]
		odds5:=data["韦德"]
		odds6:=data["易胜博"]
		odds7:=data["明陞"]
		odds8:=data["10BET"]
		odds9:=data["金宝博"]
		odds10:=data["12bet/大发"]
		odds11:=data["利记"]
		odds12:=data["盈禾"]
		odds13:=data["18Bet"]

		cMatch:=[]string{
			"A",
			"B",
			"C",
			"D",
			"E",
			"F",
			"G",
			"H",
			"I",
			"J",
		}
		c1:=[]string{
			"J",
			"K",
			"L",
			"M",
			"N",
			"O",
		}
		c2:=[]string{
			"P",
			"Q",
			"R",
			"S",
			"T",
			"U",
		}
		c3:=[]string{
			"V",
			"W",
			"X",
			"Y",
			"Z",
			"AA",
		}
		c4:=[]string{
			"AB",
			"AC",
			"AD",
			"AE",
			"AF",
			"AG",
		}
		c5:=[]string{
			"AH",
			"AI",
			"AJ",
			"AK",
			"AL",
			"AM",
		}
		c6:=[]string{
			"AN",
			"AO",
			"AP",
			"AQ",
			"AR",
			"AS",
		}
		c7:=[]string{
			"AT",
			"AU",
			"AV",
			"AW",
			"AX",
			"AY",
		}
		c8:=[]string{
			"AZ",
			"BA",
			"BB",
			"BC",
			"BD",
			"BE",
		}
		c9:=[]string{
			"BF",
			"BG",
			"BH",
			"BI",
			"BJ",
			"BK",
		}
		c10:=[]string{
			"BL",
			"BM",
			"BN",
			"BO",
			"BP",
			"BQ",
		}
		c11:=[]string{
			"BR",
			"BS",
			"BT",
			"BU",
			"BV",
			"BW",
		}
		c12:=[]string{
			"BX",
			"BY",
			"BZ",
			"CA",
			"CB",
			"CC",
		}
		c13:=[]string{
			"CD",
			"CE",
			"CF",
			"CG",
			"CH",
			"CI",
		}

		for k,v:=range match{
			index:=cMatch[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds1{
			index:=c1[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds2{
			index:=c2[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds3{
			index:=c3[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds4{
			index:=c4[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds5{
			index:=c5[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds6{
			index:=c6[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
		for k,v:=range odds7{
			index:=c7[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
		for k,v:=range odds8{
			index:=c8[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
		for k,v:=range odds9{
			index:=c9[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
		for k,v:=range odds10{
			index:=c10[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
		for k,v:=range odds11{
			index:=c11[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
		for k,v:=range odds12{
			index:=c12[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
		for k,v:=range odds13{
			index:=c13[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
	})
	err := xlsx.SaveAs("./Workbook.xlsx")
	if err != nil {
		fmt.Println(err)
	}
}